package com.astar.controller.sort;


import com.astar.dto.result.DefaultResult;
import com.astar.entity.CustomQuestion;
import com.astar.entity.PraCouChapQuestion;
import com.astar.entity.PractiseCourseChapter;
import com.astar.entity.PractiseCourseType;
import com.astar.service.IPraCouChapQuestionService;
import com.astar.service.IPractiseCourseChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类练习-科目-章节-题目  前端控制器
 * 题目管理
 * </p>
 *
 * @author jobob
 * @since 2020-01-31
 */
@Controller
@RequestMapping("/subjectManage")
public class PraCouChapQuestionController {

    @Autowired
    private IPraCouChapQuestionService praCouChapQuestionServiceImpl;

    @Autowired
    private IPractiseCourseChapterService practiseCourseChapterServiceImpl;

    @RequestMapping("/index")
    public Object toIndex(CustomQuestion customQuestion,Model model){
        List<PractiseCourseType> course = practiseCourseChapterServiceImpl.selectCourse();
        List<CustomQuestion> questionsType = praCouChapQuestionServiceImpl.selectQuestionType();
        List<PractiseCourseChapter> chapter = praCouChapQuestionServiceImpl.selectChapter();
        model.addAttribute("course",course);
        model.addAttribute("chapter",chapter);
        model.addAttribute("types",questionsType);
        return "subjectManage/index";
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public Object selectList(CustomQuestion customQuestion){
        List<CustomQuestion> questions = praCouChapQuestionServiceImpl.selectAll(customQuestion);
        return questions;
    }

    @RequestMapping("/changeChapter")
    @ResponseBody
    public Object changeChapter(Integer id){
        List<CustomQuestion> questions = praCouChapQuestionServiceImpl.changeChapter(id);
        return DefaultResult.buildDataResult(questions);
    }


    @RequestMapping("/toAdd")
    public Object toAdd(Model model){
        List<PractiseCourseChapter> chapter = praCouChapQuestionServiceImpl.selectChapter();
        model.addAttribute("chapters",chapter);
        return "subjectManage/add";
    }


    @RequestMapping("/add")
    @ResponseBody
    public Object add(PraCouChapQuestion praCouChapQuestion){
        int i = praCouChapQuestionServiceImpl.insert(praCouChapQuestion);
        return DefaultResult.buildDataResult(i);
    }

    @RequestMapping("/deletes")
    @ResponseBody
    public Object deleteForIds(String arr){
        String[] array = arr.split(",");
        int g = 0;
        for (int i =0;i<array.length;i++){
            int id = Integer.parseInt(array[i]);
            PraCouChapQuestion praCouChapQuestion = new PraCouChapQuestion();
            praCouChapQuestion.setId(id);
            praCouChapQuestionServiceImpl.deleteForIds(praCouChapQuestion);
        }
        return DefaultResult.buildDataResult(g);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object deleteForId(PraCouChapQuestion praCouChapQuestion){
        int g = praCouChapQuestionServiceImpl.deleteForIds(praCouChapQuestion);
        return DefaultResult.buildDataResult(g);
    }

    @RequestMapping("/view")
    public Object view(Integer id,Model model){
        CustomQuestion customQuestion = praCouChapQuestionServiceImpl.view(id);
        List<String> imgList = new ArrayList<>();
        String imgUrl = customQuestion.getQuestionImageUrl();
        if (imgUrl!=null && !imgUrl.equals("")){
            String[] imgArray = imgUrl.split(",");
            if (imgArray.length>0) {
                for(int i = 0; i < imgArray.length; i++) {
                    imgList.add(imgArray[i]);
                }
            }
        }
        model.addAttribute("imgList",imgList);
        model.addAttribute("customQuestion",customQuestion);
        return "subjectManage/view";
    }


    @RequestMapping("/toUpdate")
    public Object toUpdate(Integer id,Model model){
        CustomQuestion customQuestion = praCouChapQuestionServiceImpl.view(id);
        model.addAttribute("customQuestion",customQuestion);
        List<PractiseCourseChapter> chapter = praCouChapQuestionServiceImpl.selectChapter();
        model.addAttribute("chapters",chapter);
        return "subjectManage/update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(PraCouChapQuestion praCouChapQuestion){
        return DefaultResult.buildDataResult(praCouChapQuestionServiceImpl.updateForId(praCouChapQuestion));
    }



}
